package com.brkj.communityStudy;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.brkj.communityStudy.model.DS_QAStudy;
import com.brkj.httpInterface.BaseAjaxParams;
import com.brkj.httpInterface.HttpInterface;
import com.brkj.main.R;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.util.MyApplication;
import com.brkj.util.view.BaseActionBarActivity;
import com.brkj.util.view.BaseActionBarItem;
import com.brkj.util.view.BaseCoreActivity;
import com.dgl.sdk.view.PullListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.tsz.afinal.FinalHttp;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by colin.ding on 2014/5/16.
 */
public class QAStudyActivity extends BaseActionBarActivity{

    private PullListView listView;
    private TextView noData;
    private LinearLayout systemView;

    private List<DS_QAStudy> qaList;    // 所有问答
    private List<DS_QAStudy> myQaList;  //我的问答
    private List<DS_QAStudy> classQaList;  //体系问答
    private QAAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setAnimType(RIGHT_ENTER_LEFT_EXT);
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.topic_study);

        listView = (PullListView) findViewById(R.id.listView);
        noData = (TextView) findViewById(R.id.noData);
        systemView = (LinearLayout) findViewById(R.id.systemView);

        noData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getQA();
            }
        });

        initTitle();

        getQA();

        getMyQA();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(QAStudyActivity.this, QADetailActivity.class);
                intent.putExtra("QA", (Serializable)qaList.get(position-1));
                startActivity(intent);

            }
        });


        SystemStruct systemStruct = new SystemStruct(this, new SystemStruct.SystemStructItemClick() {
            @Override
            public void onClick(int classID, String systemName) {
                getQAByID(classID);
            }
        });

        systemView.addView(systemStruct.getView());
    }

    private void initTitle() {
        setReturnBtn();

        addTitleButtonItem("社区问答", new BaseActionBarItem.itemClick() {
            @Override
            public void onClick() {
                if((qaList!= null) && (qaList.size()>0)){
                    QAAdapter adapter = new QAAdapter(QAStudyActivity.this, qaList);

                    listView.setAdapter(adapter);

                    listView.setVisibility(View.VISIBLE);
                    noData.setVisibility(View.GONE);
                    systemView.setVisibility(View.GONE);
                }else {

                    listView.setVisibility(View.GONE);
                    noData.setVisibility(View.VISIBLE);
                    systemView.setVisibility(View.GONE);

                    noData.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            getQA();
                        }
                    });

                }
            }
        });

        addTitleButtonItem("我的问答", new BaseActionBarItem.itemClick() {
            @Override
            public void onClick() {

                if((myQaList!= null) && (myQaList.size()>0)){
                    QAAdapter adapter = new QAAdapter(QAStudyActivity.this, myQaList);

                    listView.setAdapter(adapter);

                    listView.setVisibility(View.VISIBLE);
                    noData.setVisibility(View.GONE);
                    systemView.setVisibility(View.GONE);
                }else {

                    listView.setVisibility(View.GONE);
                    noData.setVisibility(View.VISIBLE);
                    systemView.setVisibility(View.GONE);

                    noData.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            getMyQA();
                        }
                    });
                }

            }
        });


//        addTitleButtonItem("问答体系", new BaseActionBarItem.itemClick() {
//            @Override
//            public void onClick() {
//                listView.setVisibility(View.GONE);
//                noData.setVisibility(View.GONE);
//                systemView.setVisibility(View.VISIBLE);
//            }
//        });

        setRightBtnBackgroundAndListener(R.drawable.ep_write_selector, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(QAStudyActivity.this, QAWriteActivity.class);
                startActivityForResult(intent, 1);
            }
        });
    }

    public void getMyQA() {

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetMyQA.params.UserID, MyApplication.myUserID);

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_GetMyQA.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)QAStudyActivity.this) {
            @Override
            public void onSuccess(Object t) {
                super.onSuccess(t);

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DS_QAStudy>>(){}.getType();
                myQaList = gson.fromJson((String)t, listType);

            }
        });

    }


    public void getQA() {

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetAllQA.params.UserID, MyApplication.myUserID);
        params.put(HttpInterface.HIF_GetAllQA.params.QuestionID, "0");

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_GetAllQA.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)QAStudyActivity.this) {
            @Override
            public void onSuccess(Object t) {
                super.onSuccess(t);

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DS_QAStudy>>(){}.getType();
                qaList = gson.fromJson((String)t, listType);

                if((qaList!= null) && (qaList.size()>0)){
                    adapter = new QAAdapter(QAStudyActivity.this, qaList);

                    listView.setAdapter(adapter);

                    listView.setVisibility(View.VISIBLE);
                    noData.setVisibility(View.GONE);
                }else {

                    listView.setVisibility(View.GONE);
                    noData.setVisibility(View.VISIBLE);

                }

                if(qaList.size()>5){
                    listView.setonGetMoreListener(new PullListView.OnGetMoreListener() {
                        @Override
                        public void onGetMore() {
                            getMoreQA();
                        }
                    });
                }

            }
        });

    }

    public void getMoreQA() {

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetAllQA.params.UserID, MyApplication.myUserID);
        params.put(HttpInterface.HIF_GetAllQA.params.QuestionID,
                Integer.toString(qaList.get(qaList.size()-1).getQuestionID()));

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_GetAllQA.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)QAStudyActivity.this) {
            @Override
            public void onSuccess(Object t) {
                super.onSuccess(t);

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DS_QAStudy>>(){}.getType();
                List<DS_QAStudy> tmpQaList = gson.fromJson((String)t, listType);

                if((tmpQaList!= null) && (tmpQaList.size()>0)){
                    qaList.addAll(tmpQaList);
                    adapter.notifyDataSetChanged();
                }else{
                    showToast("没有更多问答！");
                }

                listView.onGetMoreComplete();
            }
        });

    }

    public void getQAByID(final int classID) {

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetQAbyID.params.ClassInfoID, Integer.toString(classID));

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_GetQAbyID.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)QAStudyActivity.this) {
            @Override
            public void onSuccess(Object t) {
                super.onSuccess(t);

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DS_QAStudy>>(){}.getType();
                classQaList = gson.fromJson((String)t, listType);

                if((classQaList!= null) && (classQaList.size()>0)){
                    QAAdapter adapter = new QAAdapter(QAStudyActivity.this, classQaList);

                    listView.setAdapter(adapter);

                    listView.setVisibility(View.VISIBLE);
                    noData.setVisibility(View.GONE);
                    systemView.setVisibility(View.GONE);
                }else {

                    listView.setVisibility(View.GONE);
                    systemView.setVisibility(View.GONE);
                    noData.setVisibility(View.VISIBLE);

                    noData.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            getQAByID(classID);
                        }
                    });
                }


            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        getQA();
    }
}
